package org.aj.web.exception;


import org.aj.web.exception.alarm.AlarmHandler;
import org.aj.web.exception.alarm.MailHandler;
import org.aj.web.exception.error.CustomErrorAttributes;
import org.aj.web.exception.prop.MailProperties;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.lang.Nullable;
import org.springframework.mail.javamail.JavaMailSender;

import java.util.ArrayList;
import java.util.List;

/**
 * @author jcj
 * @description自动配置类
 * date  2021/4/11 21:49
 */
@Configuration
@ConditionalOnWebApplication // 指明这是web应用，才会生效
// Load before the main WebMvcAutoConfiguration so that the error View is available
@AutoConfigureBefore(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(CustomProperties.class) // 让属性文件生效
public class CustomAutoConfiguration {


    private final CustomProperties customProperties;


    private final Environment environment;


    private final JavaMailSender javaMailSender;
    //目前为了方便获取spring容易里面的bean
    private final ApplicationContext applicationContext;

    private  List<AlarmHandler> alarmHandlers = new ArrayList<>();

    public CustomAutoConfiguration(CustomProperties customProperties, Environment environment,@Nullable JavaMailSender javaMailSender
            ,@Nullable ApplicationContext applicationContext
            ,@Nullable List<AlarmHandler> alarmHandlers) {
        this.customProperties = customProperties;
        this.environment = environment;
        this.javaMailSender = javaMailSender;
        this.applicationContext = applicationContext;
        if(null != alarmHandlers){
            this.alarmHandlers = alarmHandlers;
        }
    }




    @Bean
    public CustomCatchException customCatchException(){

        CustomCatchException customCatchException = new CustomCatchException(environment,customProperties);

        if(null != customProperties.getOrder()){
            customCatchException.setOrder(customProperties.getOrder());
        }
        customCatchException.setWebContainerInitListener(webContainerInitListener());
        if(needMailAlarm()){
            MailHandler mailHandler = new MailHandler(javaMailSender, applicationContext, customProperties.getAlarmProperties());
            alarmHandlers.add(mailHandler);
        }
        customCatchException.setAlarmHandlers(alarmHandlers);
        return customCatchException;
    }

    @Bean
    public WebContainerInitListener webContainerInitListener(){
        return new WebContainerInitListener();
    }

    @Bean
    public CustomErrorAttributes customErrorAttributes(){
        return new CustomErrorAttributes();
    }


  /*
    @Bean
    public AlarmHandler mailHandler(){
        MailHandler mailHandler = new MailHandler(javaMailSender, applicationContext, customProperties.getAlarmProperties());
        return mailHandler;
    }*/

    public boolean needMailAlarm() {
        if(null == customProperties.getAlarmProperties()){
            return false;
        }
        MailProperties mailProperties = customProperties.getAlarmProperties().getMailProperties();
        if(null != mailProperties){
            mailProperties.checkArg();
        }
        return null != javaMailSender &&  null != mailProperties && null != mailProperties.getReceiveMailAddr()
                && mailProperties.getReceiveMailAddr().length > 0;
    }

}
